/*   The MIT License
 *
 *   Carina Engine
 *   Copyright (c) 2009 2010 2011 2012 Zdravko Velinov
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#ifndef _CARINA_CSP_AST_HH_
#define _CARINA_CSP_AST_HH_

#include "carina/common/global.hh"
#include "carina/ast.hh"

namespace Carina
{
namespace CSP
{
using namespace AST;

enum NodeType
{
    CE_CSP_PROGRAM = CE_AST_NODES,
    CE_CSP_PARAMETER,
    CE_CSP_MATERIAL,
    CE_CSP_COMMON_VARIABLES,
    CE_CSP_VARIABLE,
    CE_CSP_TEXTURE,
    CE_CSP_PROFILE
};
class Program;
class Parameter;
class Material;
class CommonVariables;
class Profile;
class Variable;
class Texture;
class VisitorInterface;
}

namespace AST
{
using namespace CSP;
CE_AST_NODE_INFO(Program, CE_CSP_PROGRAM, CSP::VisitorInterface)
CE_AST_NODE_INFO(Parameter, CE_CSP_PARAMETER, CSP::VisitorInterface)
CE_AST_NODE_INFO(CSP::Material, CE_CSP_MATERIAL, CSP::VisitorInterface)
CE_AST_NODE_INFO(CommonVariables, CE_CSP_COMMON_VARIABLES, CSP::VisitorInterface)
CE_AST_NODE_INFO(Variable, CE_CSP_VARIABLE, CSP::VisitorInterface)
CE_AST_NODE_INFO(CSP::Texture, CE_CSP_TEXTURE, CSP::VisitorInterface)
CE_AST_NODE_INFO(Profile, CE_CSP_PROFILE, CSP::VisitorInterface)
}

namespace CSP
{
class Program: public AST::NamedList<Program>
{
public:
    Program(string name, AST::NodeT<List> _list);
     ~Program();
};

class Material: public AST::NamedList<Material>
{
public:
    Material(string name, AST::NodeT<List> _list);
     ~Material();
};

class Profile: public AST::NamedList<Profile>
{
public:
    Profile(string name, AST::NodeT<List> _list);
     ~Profile();
};

class CommonVariables: public AST::NamedList<CommonVariables>
{
public:
    CommonVariables(AST::NodeT<List> _list);
     ~CommonVariables();
};

class Variable
{
    string m_Name;
public:
    Variable(string name);
     ~Variable();

    string getNodeName() const { return m_Name; }
    
    bool isBlockStatement() const;
private:
    Variable(const Variable&);
    Variable& operator=(const Variable&);
};

class Texture
{
    string m_Name;
public:
    Texture(string name);
     ~Texture();

    string getNodeName() const { return m_Name; }
    
    bool isBlockStatement() const;
private:
    Texture(const Texture&);
    Texture& operator=(const Texture&);
};

class Parameter
{
    string     m_Name;
    string     m_Value;
public:
    Parameter(string name, string value);
     ~Parameter();

    string getNodeName() const { return m_Name; }
    string getValue() const;
    
    bool isBlockStatement() const;
private:
    Parameter(const Parameter&);
    Parameter& operator=(const Parameter&);
};

class VisitorInterface: public AST::VisitorInterface
{
public:
    virtual void visit(const Program* prog)=0;
    virtual void visit(const Material* mtl)=0;
    virtual void visit(const Profile* prof)=0;
    virtual void visit(const CommonVariables* var)=0;
    virtual void visit(const Variable* var)=0;
    virtual void visit(const Texture* tex)=0;
    virtual void visit(const Parameter* param)=0;
};

void PrintNode(AST::PrinterInfrastructure* printer, const Variable* var);
void PrintNode(AST::PrinterInfrastructure* printer, const Texture* tex);
void PrintNode(AST::PrinterInfrastructure* printer, const Parameter* param);

class Printer: public CSP::VisitorInterface
{
    AST::PrinterInfrastructure m_Printer;
public:
    Printer(std::ostream& os, size_t flags);
    virtual ~Printer();
    
    virtual void visit(const AST::Value<float>* value) override { AST::PrintNode(&m_Printer, value); }
    virtual void visit(const AST::Value<int>* value) override { AST::PrintNode(&m_Printer, value); }
    virtual void visit(const AST::Value<unsigned>* value) override { AST::PrintNode(&m_Printer, value); }
    virtual void visit(const AST::Value<bool>* value) override { AST::PrintNode(&m_Printer, value); }
    virtual void visit(const AST::Value<string>* value) override { AST::PrintNode(&m_Printer, value); }
    virtual void visit(const AST::ListElement* lst) override { AST::PrintNode(this, &m_Printer, lst); }
    virtual void visit(const AST::Block* _block) override { AST::PrintNode(this, &m_Printer, _block);}
    virtual void visit(const AST::StringLiteral* value) override { AST::PrintNode(&m_Printer, value); }
    virtual void visit(const Program* prog) override { prog->printList(this, &m_Printer, "program");}
    virtual void visit(const Material* mtl) override { mtl->printList(this, &m_Printer, "material"); }
    virtual void visit(const Profile* prof) override { prof->printList(this, &m_Printer, "profile");}
    virtual void visit(const CommonVariables* var) override { var->printList(this, &m_Printer, "common"); }
    virtual void visit(const Variable* var) override { PrintNode(&m_Printer, var); }
    virtual void visit(const Texture* tex) override { PrintNode(&m_Printer, tex); }
    virtual void visit(const Parameter* param) override { PrintNode(&m_Printer, param); }
};
}
}

#endif /* _CARINA_CSP_AST_HH_ */
